import options from '@/http/request.js';
import {
	getTicketOrderPay,
	setTicketPaySuccess
} from "@/http/index.js";

export function addWcOrderApi(data = {}) { //订单新增接口
	return options.reqJson('post', `/wcOrder`, data);
}
export async function payWcOrder(data = {}) { //支付
	try{
		uni.showLoading({
			title:'加载中..'
		})
		let res1 = await getTicketOrderPay({
			payAmount: data.totalPrice + '', //总金额
			thirdOrderId: data.orderId, //订单编号
			thirdType: '4',
			payPattern: '1'
		});
		let codelist = res1.data.weChatPayDTO;
		let res2 = await uni.requestPayment({
			provider: 'wxpay', //
			timeStamp: codelist.timeStamp,
			nonceStr: codelist.nonceStr,
			package: codelist.packageStr,
			signType: 'MD5', //不可变
			paySign: codelist.paySign
		});
		console.log('微信支付回调：',res2);
		if(res2.length == 1){
			throw ("取消支付");
		}
		let res3 = await setTicketPaySuccess({
			thirdOrderId: data.orderId,
			thirdType: '4',
			wxPayType: '2'
		});
		uni.showToast({
			title: '支付成功！',
			icon: 'success'
		});
		return 'success'
	}catch(e){
		console.error("支付报错culturalCreativity",e);
		uni.hideLoading();
		return 'error'
		//TODO handle the exception
	}
}

export function listLogistics(orderNumber) { //文创商城物流轨迹
	return options.reqJson('get', `/wcStockoutLogistics/detail/${orderNumber}`);
}
